#!/bin/bash
#上限文件数、上限空间数
[ -z $1 ] && FileNumberUpperLimit=100 || FileNumberUpperLimit=$1
[ -z $2 ] && FileSizeUpperLimit=10240 || FileSizeUpperLimit=$2
#统计目录下文件数
CurrentFileNumber=`ls /tmp -1 | wc -l` #文件数
CurrentFileSize=`du /tmp -s | awk '{print $1}'` #占用空间
if [ $CurrentFileNumber -ge $FileNumberUpperLimit ]
then
	echo "`date '+%Y-%m-%d %H:%M:%S'` - ExceedFileNumberUpperLimit" >> AutoDeleteFile.log
	while [ $CurrentFileNumber -ge $FileNumberUpperLimit ]
	do
		FileName="/tmp/"`ls /tmp --time=ctime | head -1`
		[ -d "$FileName" ] || rm "$FileName"
		[ $? -eq 0 ] && echo "`date '+%Y-%m-%d %H:%M:%S'` - DeleteFile: $FileName" >> AutoDeleteFile.log && CurrentFileNumber=$(($CurrentFileNumber - 1))
	done
elif [ $CurrentFileSize -ge $FileSizeUpperLimit ]
then
	echo "`date '+%Y-%m-%d %H:%M:%S'` - ExceedFileSizeUpperLimit" >> AutoDeleteFile.log
	while [ $CurrentFileSize -ge $FileSizeUpperLimit ]
	do
		FileName="/tmp/"`ls /tmp --time=ctime | head -1 ` #截取列
		[ -d "$FileName" ] || rm "$FileName" #非目录——删除
		[ $? -eq 0 ] && echo "`date '+%Y-%m-%d %H:%M:%S'` - DeleteFile: $FileName"  >> AutoDeleteFile.log #删成功输出至日志
		CurrentFileSize=`du /tmp -s | awk '{print $1}'`
		echo $CurrentFileSize		
	done
else
	echo " `date '+%Y-%m-%d %H:%M:%S'` - Not Exceed Limit" >> AutoDeleteFile.log
fi
